import { Logger } from "../../engine/utils/Logger";
import FishBulletBase from "../../../fish/script/FishBulletBase";
import FishMathUtils from "../../engine/utils/FishMathUtils";
import CannonManager from "./CannonManager";
import { MoveHelper } from "../../engine/utils/MoveHelper";
import FishNetManager from "./FishNetManager";
import GameMusicHelper from "../utils/GameMusicHelper";
import FishUI from "../../../fish/script/FishUI";
import CommonTips from "../../engine/uicomponent/CommonTips";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BulletManager extends cc.Component {

    public static instance: BulletManager = null;

    @property({ type: [cc.Prefab] })
    private bulletPrefabList: cc.Prefab[] = [];

    private bulletPool: Array<cc.NodePool> = [];

    private bulletList: Array<FishBulletBase> = [];


    private bulletMoveSpeed: number = 30;

    onLoad() {
        BulletManager.instance = this;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onShootBullet.bind(this))
    }

    start() {

    }

    update() {
        this.checkMoveBullet();
    }

    private checkMoveBullet() {
        for (let i = this.bulletList.length - 1; i >= 0; i--) {
            let bullet: FishBulletBase = this.bulletList[i];
            let isMoving: boolean = MoveHelper.moveNode(bullet.node, this.bulletMoveSpeed, bullet.targetP.x, bullet.targetP.y)
            if (!isMoving) {
                FishNetManager.instance.addFishNet(bullet.bulletType, bullet.node.getPosition())
                this.bulletList.splice(i, 1)
                this.destroyBullet(bullet)
            }
        }
    }

    private onShootBullet(event: cc.Event.EventTouch) {
        let localP: cc.Vec2 = this.node.convertToNodeSpaceAR(event.getLocation())
        FishUI.instance.playClickEffect(localP)
        if (FishUI.instance.score >= CannonManager.instance.cannonType) {
            FishUI.instance.score -= CannonManager.instance.cannonType;
            FishUI.instance.refreshScore();
            let rad: number = FishMathUtils.p2pRad(CannonManager.instance.getCannonPosition(), localP)
            let rot: number = FishMathUtils.radiansToDegrees(rad)
            let bullet: FishBulletBase = this.createBullet(CannonManager.instance.cannonType - 1)
            bullet.targetP = localP;
            this.node.addChild(bullet.node)
            bullet.node.setPosition(CannonManager.instance.getCannonPosition())
            bullet.node.setScale(2)
            bullet.node.angle = rot;
            this.bulletList.push(bullet)
            GameMusicHelper.playFire();
        } else {
            CommonTips.showMsg("金币不足!")
        }
    }

    private createBullet(bulletType: number) {
        let bulletNode: cc.Node;
        if (this.bulletPool[bulletType] && this.bulletPool[bulletType].size() > 0) {
            bulletNode = this.bulletPool[bulletType].get();
        } else {
            bulletNode = cc.instantiate(this.bulletPrefabList[bulletType])
        }
        bulletNode.getComponent(FishBulletBase).bulletType = bulletType;
        return bulletNode.getComponent(FishBulletBase)
    }

    public killBullet(bullet: FishBulletBase) {
        let index: number = this.bulletList.indexOf(bullet);
        if (index >= 0) {
            this.bulletList.splice(index, 1);
            this.destroyBullet(bullet)
        }
    }

    private destroyBullet(bullet: FishBulletBase) {
        if (!this.bulletPool[bullet.bulletType]) {
            this.bulletPool[bullet.bulletType] = new cc.NodePool();
        }
        this.bulletPool[bullet.bulletType].put(bullet.node)
    }

    onDestroy() {
        BulletManager.instance = null;
    }

}
